package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.User1;
import com.cskaoyan.bean.collect.AddorDeleteBean;
import com.cskaoyan.bean.collect.CollectListRequestBean;
import com.cskaoyan.bean.collect.WxCollectBean;
import com.cskaoyan.service.WxCollectService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("wx/collect")
public class WxCollectController {

    @Autowired
    WxCollectService wxCollectService;

    @RequestMapping("list")
    public BaseRespVo list( CollectListRequestBean collectListRequestBean){
        WxCollectBean wxCollectBean = wxCollectService.SelectCollectlist(collectListRequestBean);
        return BaseRespVo.ok(wxCollectBean);
    }

    // ==> xiugai
    @RequestMapping("addordelete")
    public BaseRespVo addordelete(@RequestBody  AddorDeleteBean addorDeleteBean){
        //先查询collect表是否含有这条数据，没有则添加，有则将deleted改为相反状态
        Subject subject = SecurityUtils.getSubject();
        User1 user = (User1) subject.getPrincipal();
        if (user == null) {
            return BaseRespVo.fail(501, "请登录");
        }
        Integer userid = user.getId();
        Integer isHaveCollect = wxCollectService.selectIsHaveCollect(addorDeleteBean,userid);
        String type = null;
        if(isHaveCollect==1){   //collect表中已包含这条记录，shanchu
           wxCollectService.deleteCollect(addorDeleteBean,userid);
            type = "delete";
            return BaseRespVo.ok(type);
        }
        else{   //没有-> 插入数据 (deleted=0)
            wxCollectService.insertCollect(addorDeleteBean,userid);
            type= "add";
            return BaseRespVo.ok(type);
        }
//        Integer deleted = wxCollectService.selecteDeleted(addorDeleteBean,userid);
//        if(deleted==1){
//            type = "delete";
//        }else{
//            type= "add";
//        }
//        return BaseRespVo.ok(type);
    }
}
